package 耿翔宇;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Collectors;

import cn.hutool.core.io.FileUtil;

public class PPP {

	public static void main(String[] args) {
		String path = "user.txt";
		login(path);
	}

	private static void login(String path) {
		List<String> usersInfo = FileUtil.readLines(new File(path), "utf-8");
		Map<String, String> userStatus = usersInfo.stream().collect(Collectors.toMap(s->s.split("=")[0], s->s.split("=")[2]));
		Map<String, String> userPassword = usersInfo.stream().collect(Collectors.toMap(s->s.split("=")[0],s->s.split("=")[1]));
		Scanner sc = new Scanner(System.in);
		System.out.println("�����û�����");
		String name = sc.next();
		if(!userPassword.containsKey(name)){
			System.out.println("�û��������ڣ�����������");
		}else if(userStatus.get(name).equals("-1")){
			System.out.println("���˻��ѱ����ᣬ��ѡ�������û���¼��");
		}else{
			System.out.println("�������룺");
			String password = sc.next();
			if(!userPassword.get(name).equals(password)){
				int oldStatus = Integer.parseInt(userStatus.get(name));
				if(oldStatus<2 || (oldStatus>=3 && oldStatus<=4)){
					System.out.println("�����������������");
					String newStatus = oldStatus+1+"";
					userStatus.put(name, newStatus);
					//д��
					writeBack(usersInfo,userStatus);
				}else if(oldStatus==2){
					System.out.println("����������࣬10���ſ����ٴ�����");
					String newStatus = 3+"";
					userStatus.put(name, newStatus);
					//д��
					writeBack(usersInfo,userStatus);
				}else if(oldStatus==5){
					System.out.println("�����������������࣬�˻��Ѷ���");
					String newStatus = -1+"";
					userStatus.put(name, newStatus);
					//д��
					writeBack(usersInfo,userStatus);
				}
			}else{
				System.out.println("��¼�ɹ�");
				//д��
		        for (String key : userStatus.keySet()) {
		        	if(!userStatus.get(key).equals("-1")){
		        		userStatus.put(key, "0");
		        	}		        	
		        }
				writeBack(usersInfo,userStatus);
			}
		}
	}

	private static void writeBack(List<String> usersInfo, Map<String, String> userStatus) {
		ArrayList<String> newUsers = new ArrayList<String>();
		for(String user : usersInfo){
			if(!user.endsWith("-1")){
		        StringBuilder sb = new StringBuilder(user);	        
		        sb.replace(user.length() - 1, user.length(), userStatus.get(user.split("=")[0]));
		        newUsers.add(sb.toString());
			}else{
				newUsers.add(user);
			}
		}
		FileUtil.writeLines(newUsers, new File("user.txt"), "utf-8");
	}	

}
